package com.realbrand.net.realbrandpay.ui.activity.wallet.set;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.base.FirstActivity;
import com.realbrand.net.realbrandpay.ui.activity.ADSActivity;
import com.realbrand.net.realbrandpay.ui.activity.OneActivity;
import com.realbrand.net.realbrandpay.ui.activity.login.LoginActivity;
import com.realbrand.net.realbrandpay.ui.activity.mall.MallActivity;
import com.realbrand.net.realbrandpay.ui.activity.shopcar.ShopCarActivity;
import com.realbrand.net.realbrandpay.untils.Constant;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.app.CaptureActivity;
import com.realbrand.net.realbrandpay.zxing.util.L;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/31.
 */

public class WalletLogoutActivity extends FirstActivity {
    @BindView(R.id.walletlogout_bt_login)
    Button walletlogoutBtLogin;
    @BindView(R.id.walletLoginoutActivity_liner_withdraw)
    LinearLayout walletlogoutBtWithdraw;
    @BindView(R.id.walletLoginoutActivity_liner_scan)
    LinearLayout walletlogoutBtScan;
    @BindView(R.id.walletLoginoutActivity_liner_bill)
    LinearLayout walletlogoutBtBill;
    @BindView(R.id.walletLoginoutActivity_liner_personal)
    LinearLayout walletlogoutBtAccount;
    @BindView(R.id.walletlogout_toolbar_tv_setting)
    TextView walletlogoutToolbarTvSetting;
    @BindView(R.id.walletlogout_toolbar)
    Toolbar walletlogoutToolbar;
    @BindView(R.id.wallet_tab_scan)
    TextView walletTabScan;
    @BindView(R.id.wallet_tab_mall)
    TextView walletTabMall;
    @BindView(R.id.wallet_tab_shop)
    TextView walletTabShop;
    SharedPreferences sharedPreferences;
    public AlertDialog dialog;
    //是否选择不要提示
    public boolean isCheck;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        walletlogoutToolbar.setTitle("");
        setSupportActionBar(walletlogoutToolbar);
        //判断是否弹出更新版本提示
//        sharedPreferences = getSharedPreferences(Constant.shareName, MODE_PRIVATE);
//        int VersionCompare = sharedPreferences.getInt(Constant.VersionCompare, 0);
//        int VersionUpData = sharedPreferences.getInt(Constant.VersionUpData, 0);
//        String VersionUpDataUrl = sharedPreferences.getString(Constant.VersionUpDataUrl, "");
//        if (VersionCompare < VersionUpData && CaptureActivity.aBoolean) {
//            L.d(L.TAG, "ture" + CaptureActivity.aBoolean);
//            //提示版本更新
//            if (!VersionUpDataUrl.equals("")) {
//                updataDialog(VersionUpDataUrl, VersionUpData);
//            }
//        }
        setOnClick();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.walletlogout_activirty;
    }

    @Override
    protected void setOnClick() {
        walletlogoutBtAccount.setOnClickListener(this);
        walletlogoutBtBill.setOnClickListener(this);
        walletlogoutBtLogin.setOnClickListener(this);
        walletlogoutBtScan.setOnClickListener(this);
        walletlogoutBtWithdraw.setOnClickListener(this);
        walletlogoutToolbarTvSetting.setOnClickListener(this);
        walletTabMall.setOnClickListener(this);
        walletTabScan.setOnClickListener(this);
        walletTabShop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.walletLoginoutActivity_liner_personal:
                Static.toast(this, "用户未登录，请先点击登录");
                break;
            case R.id.walletLoginoutActivity_liner_bill:
                Static.toast(this, "用户未登录，请先点击登录");
                break;
            case R.id.walletlogout_bt_login:
                Intent intent1 = new Intent(this, LoginActivity.class);
                intent1.putExtra("whereActivity", "WalletLogout");
                startActivity(intent1);
                finish();
                break;
            //订单
            case R.id.walletLoginoutActivity_liner_scan:
//                Intent intent = new Intent(WalletLogoutActivity.this, CaptureActivity.class);
//                Bundle bundle_scan = new Bundle();
//                bundle_scan.putString(Static.BundleKey, ADSActivity.VALUE_ADS);
//                intent.putExtras(bundle_scan);
//                startActivity(intent);
                Static.toast(this, "用户未登录，请先点击登录");
                break;
            case R.id.walletLoginoutActivity_liner_withdraw:
                Static.toast(this, "用户未登录，请先点击登录");
                break;
            case R.id.walletlogout_toolbar_tv_setting:
                Static.toast(this, "用户未登录，请先点击登录");
                break;
            //商城
            case R.id.wallet_tab_mall:
                Intent intent2 = new Intent(this, LoginActivity.class);
                intent2.putExtra("whereActivity", "WalletLogout");
                startActivity(intent2);
                finish();
                break;
            //扫码
            case R.id.wallet_tab_scan:
                ShopCarActivity.mBoolean = true;
                Intent intent = new Intent(this, CaptureActivity.class);
                Bundle bundle_scan = new Bundle();
                bundle_scan.putString(Static.BundleKey, ADSActivity.VALUE_ADS);
                intent.putExtras(bundle_scan);
                startActivity(intent);
                finish();
                break;
            //购物车
            case R.id.wallet_tab_shop:
                startActivity(new Intent(this, ShopCarActivity.class));
                finish();
                break;
        }


    }
    private void updataDialog(final String versionUpDataUrl, final int versionUpData) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.updata_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        dialog = builder.create();
        dialog.setView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        TextView cancel = (TextView) view.findViewById(R.id.dialog_updata_cancel);
        TextView upData = (TextView) view.findViewById(R.id.dialog_updata_updata);
        CheckBox noHint = (CheckBox) view.findViewById(R.id.dialog_updata_noHint);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CaptureActivity.aBoolean = false;
                dialog.dismiss();
            }
        });
        upData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //更新app
                Static.upDataAPP(versionUpDataUrl, versionUpData, WalletLogoutActivity.this);
                L.d(L.TAG, "点击更新");
                dialog.dismiss();
                CaptureActivity.isGetVersion = true;
                isCheck = false;
            }
        });
        noHint.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    isCheck = true;
                    CaptureActivity.isGetVersion = false;
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putInt(Constant.VersionCompare, versionUpData);
                    edit.commit();
                }
            }
        });
        if (isCheck) {
            CaptureActivity.isGetVersion = false;
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putInt(Constant.VersionCompare, versionUpData);
            edit.commit();
        }
        dialog.show();
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
